package cc.angis.xiugallery.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import cc.angis.xiugallery.data.CommentData;

public class CommentDBHelper extends BaseDBHelper {
	
	public CommentDBHelper(){
		
	}
	
	public CommentDBHelper(Context context, String table){
		super(context, table);
	}
	
	public CommentDBHelper(Context context, String table, ContentValues values){
		super(context, table, values);
	}
	
	public long delete(int id){
		mWhereClaus = "id=?";
		mWhereArgs = new String[] { String.valueOf(id) };
		return delDB();
	}
	
	public long deleteByPostId(int postid){
		mWhereClaus = "postid=?";
		mWhereArgs = new String[] { String.valueOf(postid) };
		return delDB();
	}
	
	public long insert(String commentid, String postid, String content, String author, String avatar, long time, int type){
		mValues = new ContentValues();
		mValues.put(TableConstants.CommentColumn.COMMENT_ID, commentid);
		mValues.put(TableConstants.CommentColumn.POST_ID, postid);
		mValues.put(TableConstants.CommentColumn.CONTENT, content);
		mValues.put(TableConstants.CommentColumn.AUTHOR, author);
		mValues.put(TableConstants.CommentColumn.AVATAR, avatar);
		mValues.put(TableConstants.CommentColumn.COMMENT_TIME, time);
		mValues.put(TableConstants.CommentColumn.COMMENT_TYPE, type);
		return insertDB();
	}
	
	public CommentData getLastCommentByType(int type){
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, new String[]{TableConstants.CommentColumn.COMMENT_ID, TableConstants.CommentColumn.COMMENT_TIME}, 
				"commenttype=?", new String[]{String.valueOf(type)}, null, null, TableConstants.CommentColumn.COMMENT_TIME + " DESC", "1");
		int commentIdColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.COMMENT_ID);
		int commentTimeIdColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.COMMENT_TIME);
		CommentData comment = new CommentData();;
		if (cursor.moveToFirst()){
			do {
				comment.setCommentId(cursor.getString(commentIdColumn));
				comment.setTime(cursor.getLong(commentTimeIdColumn));
			}while(cursor.moveToNext());
		}
		cursor.close();
		return comment;
	}
	
	public List<CommentData> getAllCommentsByPostId(int postId){
		List<CommentData> commentDatas = new ArrayList<CommentData>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, null, "postid=?", new String[]{String.valueOf(postId)}, null, null, TableConstants.CommentColumn.COMMENT_TIME + " DESC");
		int idColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.ID);
		int postidColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.POST_ID);
		int contentColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.CONTENT);
		int authorColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.AUTHOR);
		int avatarColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.AVATAR);
		int typeColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.COMMENT_TYPE);
		int timeColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.COMMENT_TIME);
		if (cursor.moveToFirst()){
			CommentData commentData;
			do {
				commentData = new CommentData();
				commentData.setId(cursor.getInt(idColumn));
				commentData.setPostId(cursor.getInt(postidColumn));
				commentData.setContent(cursor.getString(contentColumn));
				commentData.setTime(cursor.getLong(timeColumn));
				commentData.setType(cursor.getInt(typeColumn));
				commentData.setAuthor(cursor.getString(authorColumn));
				commentData.setAvatar(cursor.getString(avatarColumn));
				commentDatas.add(commentData);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return commentDatas;
	}
	
	public CommentData getLastCommentByPostId(String postId){
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, null, "postid=?", new String[]{postId}, null, null, TableConstants.CommentColumn.COMMENT_TIME + " DESC", "1");
		int idColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.ID);
		int postidColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.POST_ID);
		int contentColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.CONTENT);
		int authorColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.AUTHOR);
		int avatarColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.AVATAR);
		int typeColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.COMMENT_TYPE);
		int timeColumn = cursor.getColumnIndex(
				TableConstants.CommentColumn.COMMENT_TIME);
		CommentData commentData = null;
		if (cursor.moveToFirst()){
			do {
				commentData = new CommentData();
				commentData.setId(cursor.getInt(idColumn));
				commentData.setPostId(cursor.getInt(postidColumn));
				commentData.setContent(cursor.getString(contentColumn));
				commentData.setTime(cursor.getLong(timeColumn));
				commentData.setType(cursor.getInt(typeColumn));
				commentData.setAuthor(cursor.getString(authorColumn));
				commentData.setAvatar(cursor.getString(avatarColumn));
			}while(cursor.moveToNext());
		}
		cursor.close();
		return commentData;
	}
	
}
